package thread;

public class SyncDemo {
    private static boolean success1 = false;
    private static boolean success2 = false;
    public static void main(String[] args) {
        Bank bank = new Bank();
        while(true){
            Thread t1 = new Thread(()->{
                success1 = bank.getMoney(10000);
            });
            Thread t2 = new Thread(()->{
                success2 = bank.getMoney(10000);
            });
            t1.start();
            t2.start();
            try{
                Thread.sleep(100);
            }catch(InterruptedException e){
            }
            if(success1&success2){
                System.out.println("卡BUG成功");
            }else{
                System.out.println("只有一个人取款");
                bank.saveAccount(10000);
                success1 = false;
                success2 = false;
            }
        }
    }
}

class Bank{
    private int account = 10000;
    public synchronized boolean getMoney(int money){
        int account = getAccount();
        if(account>=money){
            account = account-money;
            Thread.yield();
            saveAccount(account);
            return true;
        }
        return false;
    }
    public void saveAccount(int account){
        this.account = account;
    }
    public int getAccount(){
        return account;
    }
}